package com.battle.project.order.mq.receive;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * Headers 模式 交换机路由到队列时  根据定义交换机和队列绑定关系时 定义的Map集合  和 发送消息是 消息的header中的数据 匹配规则 来进行路由
 */
@Component
public class HeadersReceiveMqHandler {

    private static Logger logger = LoggerFactory.getLogger(HeadersReceiveMqHandler.class);


    @RabbitListener(queues = {"order_headers_queue1"})
    public void receiveHeaderQueue1Message(Message message, Channel channel) {
        byte[] body = message.getBody();
        MessageProperties messageProperties = message.getMessageProperties();
        //
        long deliveryTag = messageProperties.getDeliveryTag();

        String messageStr = new String(body, StandardCharsets.UTF_8);
        logger.info("【Headers】【Queue1】ReceiveMqHandler接收到消息message:{}", messageStr);
    }

    @RabbitListener(queues = {"order_headers_queue2"})
    public void receiveHeaderQueue2Message(Message message, Channel channel) {
        byte[] body = message.getBody();
        MessageProperties messageProperties = message.getMessageProperties();
        //
        long deliveryTag = messageProperties.getDeliveryTag();

        String messageStr = new String(body, StandardCharsets.UTF_8);
        logger.info("【Headers】【Queue2】ReceiveMqHandler接收到消息message:{}", messageStr);
    }
}
